package com.firemoss.atonement.changewatcher.observation
{
	import com.firemoss.atonement.changewatcher.valuecomparison.DefaultValueComparitorFactory;
	import com.firemoss.atonement.changewatcher.valuecomparison.IValueComparitorFactory;
	import com.firemoss.atonement.metadata.DefaultMetadataBuilder;
	import com.firemoss.atonement.metadata.IMetadataBuilder;
	import com.firemoss.atonement.metadata.inspectors.bindability.DefaultBindabilityInspector;
	import com.firemoss.atonement.metadata.inspectors.bindability.IBindabilityInspector;
	
	import flash.events.IEventDispatcher;
	
	public class DefaultChangeObserverFactory implements IChangeObserverFactory
	{
		private var builder : IMetadataBuilder = new DefaultMetadataBuilder()
		private var compare :IValueComparitorFactory = new DefaultValueComparitorFactory()
			
		public function DefaultChangeObserverFactory()
		{
		}
		
		public function get valueComparitorFactory():IValueComparitorFactory
		{
			return compare;
		}
		
		public function createObserverFor( instance : Object ) : ChangeObserver
		{
			if ( !( instance is IEventDispatcher ) )
			{
				throw new Error("At runtime, the instance must be an event dispatcher (IEventDispatcher).  Please mark it [Bindable].");
			}
			
			var events : Array = builder.buildMetadata( instance ).propertyChangeEvents
				
			if ( events == null || events.length == 0 )
			{
				throw new Error("Can't create an observer for an instance that isn't [Bindable] in any way, shape, or form.");
			}
			
			return new ChangeObserver( IEventDispatcher( instance ), events, valueComparitorFactory );
		}
	}
}